package com.linciping.useges.plugin.search;

import com.linciping.useges.plugin.listener.SourceFileFilter;
import com.linciping.useges.plugin.util.CollectionUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

public class UsegesFilter implements SourceFileFilter {

    private AtomicReference<String> query = new AtomicReference<>();
    private final String rootPackageName;
    private final AtomicBoolean isInitCache = new AtomicBoolean(false);
    private final ConcurrentHashMap<Integer, List<String>> allowPackageList;

    public UsegesFilter(String rootPackageName) {
        this.allowPackageList = new ConcurrentHashMap<>();
        this.rootPackageName = rootPackageName;
    }

    public void updateQuery(String query) {
        this.query.set(query);
    }

    public void firstForeachEnd() {
        isInitCache.set(true);
    }

    private boolean findInCache(List<String> packageNameList) {
        for (String packageName : packageNameList) {
            if (packageName.contains(query.get())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean isFilterFile(int fileHashcode, List<String> importPackageList) {
        if (!isInitCache.get()) {
            return findImport(fileHashcode, importPackageList);
        } else {
            return allowPackageList.containsKey(fileHashcode) && findInCache(allowPackageList.get(fileHashcode));
        }
    }

    private boolean findImport(int fileHashcode, List<String> importPackageList) {
        if (CollectionUtil.isListEmpty(importPackageList)) {
            return false;
        }
        List<String> importPackageNameList = new ArrayList<>();
        for (String importPackage : importPackageList) {
            if (importPackage.contains(rootPackageName)) {
                importPackageNameList.add(importPackage);
            }
        }
        if (!CollectionUtil.isListEmpty(importPackageNameList)) {
            allowPackageList.put(fileHashcode, importPackageNameList);
        }
        for (String importPackageName : importPackageNameList) {
            if (importPackageName.contains(query.get())) {
                return true;
            }
        }
        return false;
    }

//    private boolean findJavaImport(PsiJavaFile psiJavaFile, int key) {
//        if (!PsiFileUtil.isAllowJavaFile(psiJavaFile)) {
//            return false;
//        }
//        PsiImportStatement[] psiImportStatements = psiJavaFile.getImportList().getImportStatements();
//        List<String> importPackageNameList = new ArrayList<>();
//        for (PsiImportStatement psiImportStatement : psiImportStatements) {
//            String importText = psiImportStatement.getText();
//            if (importText.contains(rootPackageName)) {
//                importPackageNameList.add(importText);
//            }
//        }
//        if (!CollectionUtil.isListEmpty(importPackageNameList)) {
//            allowPackageList.put(key, importPackageNameList);
//        }
//        for (String importPackageName : importPackageNameList) {
//            if (importPackageName.contains(query.get())) {
//                return true;
//            }
//        }
//        return false;
//    }
//
//    private boolean findKtImport(KtFile ktFile, int key) {
//        if (!PsiFileUtil.isAllowKtFile(ktFile)) {
//            return false;
//        }
//        List<KtImportDirective> importDirectiveList = ktFile.getImportDirectives();
//        List<String> importPackageNameList = new ArrayList<>();
//        for (KtImportDirective ktImportDirective : importDirectiveList) {
//            String importText = ktImportDirective.getText();
//            if (importText.contains(rootPackageName)) {
//                importPackageNameList.add(importText);
//            }
//        }
//        if (!CollectionUtil.isListEmpty(importPackageNameList)) {
//            allowPackageList.put(key, importPackageNameList);
//        }
//        for (String importPackageName : importPackageNameList) {
//            if (importPackageName.contains(query.get())) {
//                return true;
//            }
//        }
//        return false;
//    }

}
